package com.niannian.util;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import android.content.Context;

public abstract class PreferenceBeanHelper {

	private PreferencesUtils preferencesUtils;

	public void init(Context context) {
		preferencesUtils = PreferencesUtils.getPreferencesUtilsInstance(context);
	}

	/** 更新属性值和和对应sp */
	public void updatePrefer(String field, Object value) throws Exception {
		Field temp = this.getClass().getDeclaredField(field);
		FileUtils.setValueToFiled(temp, this, value.toString());
		preferencesUtils.saveOne(this, temp);
	}

	public void updatePreferAll() throws Exception {
		preferencesUtils.save(this);

	}

	/** 装载sharedpreferences的数据到对象 */
	public void loadFromPref() throws Exception {
		preferencesUtils.getObjectFromSp(this.getClass(), this);
	}

	public void clearPref() throws Exception {
		preferencesUtils.clear(this);
		Field[] fields = this.getClass().getDeclaredFields();
		for (Field field : fields) {
			if (!Modifier.isFinal(field.getModifiers()))
				FileUtils.setDefValueToFiled(field, this);
		}
	}

	/** 判断从sharedpreferences加载的数据是否可用，即sharedpreferences的数据是否已过期 */
	public abstract boolean isDataInvalid();

	public abstract void setDataInvalid();

}
